import os
import sys
import django

# 设置Django环境
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'backend'))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HuaXianGuan.settings')
django.setup()

from apps.flowers.models import FlowerCategory, Flower, FlowerImage


def main():
    """主函数"""
    print("=== 鲜花数据统计 ===")
    
    # 检查分类
    categories = FlowerCategory.objects.all()
    print(f"分类总数: {categories.count()}")
    
    for category in categories:
        flowers_count = Flower.objects.filter(category=category).count()
        print(f"- {category.name}: {flowers_count} 个商品 (排序: {category.sort_order})")
    
    print(f"\n=== 鲜花商品统计 ===")
    flowers = Flower.objects.all()
    print(f"商品总数: {flowers.count()}")
    
    for flower in flowers:
        images_count = flower.images.count()
        print(f"- {flower.name}")
        print(f"  分类: {flower.category.name}")
        print(f"  价格: ¥{flower.price}")
        print(f"  库存: {flower.stock}")
        print(f"  销量: {flower.sales_count}")
        print(f"  图片数量: {images_count}")
        if flower.main_image:
            print(f"  主图: {flower.main_image}")
        print()
    
    # 检查图片文件
    print("=== 图片文件检查 ===")
    image_dir = "../frontend/images/flowers"
    if os.path.exists(image_dir):
        images = [f for f in os.listdir(image_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))]
        print(f"图片文件总数: {len(images)}")
        for image in images:
            print(f"- {image}")
    else:
        print(f"图片目录不存在: {image_dir}")


if __name__ == "__main__":
    main()